package com.whmnx.chitchat.ui.chat;

import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.qcloud.tim.uikit.component.TitleBarLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.contact.ContactLayout;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.ChatListAdapter;
import com.whmnx.chitchat.adapter.DepAdapter;
import com.whmnx.chitchat.adapter.UserAdpter;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.ChatMsgBean;
import com.whmnx.chitchat.bean.ChatMsgDatailBean;
import com.whmnx.chitchat.bean.DepartmentDean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.book.DepartmentActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.Watermark;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
/**
 * author : huang
 * date   : 2020/8/14 0014 16:09
 * version: 1.0
 * @author Administrator
 */
public class ChatMiActivity extends BaseActivity {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    @BindView(R.id.rv_chatMsg)
    RecyclerView rvChatMsg;

    private int page = 1;
    private int Rows = 10;

    private ChatListAdapter  mChatListAdapter;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.chat_mi_activity);
        setRringTitle("+");
        tv_right.setTextSize(30);
        setTitle("密聊");
        initView();
        String com = SPUtil.getString(Constants.COM);
        String dep = SPUtil.getString(Constants.DEP);
        String nick = SPUtil.getString(Constants.NICKNAME);
        Watermark.getInstance().show(this, dep + com + nick);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (!messageEvent.getMessageKey().equals(EventConstants.FAMILY)) {
                getChatRecordList(true);
            }
        }
    }


    @Override
    protected void loadData() {
        tv_right.setOnClickListener(v -> startActivity(new Intent(ChatMiActivity.this,MiContactsActivity.class)
                .putExtra("isTopChat", true)));
    }

    private void initView() {
        rvChatMsg.setLayoutManager(new LinearLayoutManager(this));
        rvChatMsg.setItemAnimator(new DefaultItemAnimator());
        mChatListAdapter = new ChatListAdapter(this);
        rvChatMsg.setAdapter(mChatListAdapter);
        refresh.setOnRefreshListener(refreshLayout -> getChatRecordList(true));
        refresh.setOnLoadMoreListener(refreshLayout -> getChatRecordList(false));
        getChatRecordList(true);
    }

    private void getChatRecordList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("UserID",SPUtil.getString(Constants.USERID));
        params.put("Page",page + "");
        params.put("Rows",Rows + "");
        params.put("RecordType","1");
        HttpClient.get(this, Constants.GETCHATRECORDLIST, params,new CallBack<ChatMsgDatailBean>() {
            @Override
            public void onSuccess(ChatMsgDatailBean result) {
                if (isRefresh){
                    refresh.finishRefresh(true);
                    mChatListAdapter.replaceAll(result.List, true);
                }else {
                    if (result.List.size() > 0) {
                        refresh.finishLoadMore(true);
                        mChatListAdapter.addAll(result.List);
                    }else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });
    }
}
